<?php
/**
 * 通用方法
 * @author Administrator
 *
 */
class CCommon extends CBase {
	
	/**
	 * 密码加密方法
	 * @param string $pwd
	 */
	public static function pwdMd5($pwd)
	{
		return substr(md5($pwd), 0, 30);
	}
	
	/**
	 * 获取客户端IP
	 */
	public static function getClientIP()
	{
		$realip = "";
	    if (isset($_SERVER))
	    {
	        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
	        {
	            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
	
	            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
	            foreach ($arr AS $ip)
	            {
	                $ip = trim($ip);
	
	                if ($ip != 'unknown')
	                {
	                    $realip = $ip;
	
	                    break;
	                }
	            }
	        }
	        elseif (isset($_SERVER['HTTP_CLIENT_IP']))
	        {
	            $realip = $_SERVER['HTTP_CLIENT_IP'];
	        }
	        else
	        {
	            if (isset($_SERVER['REMOTE_ADDR']))
	            {
	                $realip = $_SERVER['REMOTE_ADDR'];
	            }
	            else
	            {
	                $realip = '0.0.0.0';
	            }
	        }
	    }
	    else
	    {
	        if (getenv('HTTP_X_FORWARDED_FOR'))
	        {
	            $realip = getenv('HTTP_X_FORWARDED_FOR');
	        }
	        elseif (getenv('HTTP_CLIENT_IP'))
	        {
	            $realip = getenv('HTTP_CLIENT_IP');
	        }
	        else
	        {
	            $realip = getenv('REMOTE_ADDR');
	        }
	    }
	
	    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
	    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
	
	    return $realip;
	}
	
	public static function getImageInfo($imageFile)
	{
		$arrInfo = @getimagesize($imageFile);

        if($arrInfo==false)
        {
        	return false;
        }
        $imgInfo['width'] = $arrInfo[0];
        $imgInfo['height'] = $arrInfo[1];

        switch ($arrInfo[2])
        {
            case 1:
                $imgInfo['type'] = "gif";
                break;
            case 2:
                $imgInfo['type'] = "jpeg";
                break;
            case 3:
                $imgInfo['type'] = "png";
                break;
            default:
                return false;
                //echo(" image type.");
                break;
        } //swith end
        return $imgInfo;
	}
	
	public function code($sessionKey) {
		$width = 97;
		$height = 38;
		$num = 4;
		$size = 24;
		$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUV";
		$code = '';
		for ($i = 0; $i < $num; $i++) {
			$code .= $str[mt_rand(0, strlen($str) - 1)];
		}
	
		// 画图像
		$im = imagecreatetruecolor($width, $height);
		// 定义要用到的颜色
		$back_color = imagecolorallocate($im, 235, 236, 237);
		$boer_color = imagecolorallocate($im, 118, 151, 199);
		$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
		// 画背景
		imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
		// 画边框
		imagerectangle($im, 0, 0, $width - 1, $height - 1, $boer_color);
		// 画干扰线
		for ($i = 0; $i < 5; $i++) {
			$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
			imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
		}
		// 画干扰点
		for ($i = 0; $i < 50; $i++) {
			$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
			imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
		}
		// 画验证码
		$aa=imagefttext($im, $size, 0, 2, $size + 3, $text_color, $this->getAbsolutePath("images") . 'arial.ttf', $code);
	
		if (!isset($_SESSION)) {
			session_start();
		}

		$_SESSION[$sessionKey] = $code;
		header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
		header("Content-type: image/png;charset=utf8");
		imagepng($im);
		imagedestroy($im);
	}
	
	/* public static function getDiscount($type)
	{
		if($type==1)
		{
			return "送积分";
		}
		else{
			return "折扣";
		}
	} */
	
	public static function checkNum($v)
	{
		$c = preg_match("/^\\d+$/", $v);
		
		return $c>0?true:false;
	}
	
	
	public static function checkMoney($v)
	{
		$c = preg_match("/^\\d{0,10}(\\.\\d{0,2})?$/", $v);
		
		return $c>0?true:false;
	}
	
	public static function checkDiscount($v)
	{
		$c = preg_match("/^0\\.\\d{1,2}$/", $v);
	
		return $c>0?true:false;
	}
	
	/**
	 * 请求的浏览器是否是IE
	 */
	public static function isIE() {
		$userAgent = isset($_SERVER["HTTP_USER_AGENT"])?$_SERVER["HTTP_USER_AGENT"]:"";
		
		if(strpos($userAgent,"MSIE")!==false)
		{
			return true;
		}
		
		return false;
	}
	
	public static function sendToMail($email,$title,$content)
	{
		try{
			$mailConfig = Config::get("mail");
			$mail = new PHPMailer(); //建立邮件发送类
			$address = "$email";
			$mail->IsSMTP(); // 使用SMTP方式发送
			$mail->Host = $mailConfig["host"]; // 您的企业邮局域名
			$mail->CharSet = "utf-8";
			$mail->Encoding = 'base64';
			$mail->SMTPAuth = true; // 启用SMTP验证功能
			$mail->Username = $mailConfig["username"]; // 邮局用户名(请填写完整的email地址)
			$mail->Password = $mailConfig["pwd"]; // 邮局密码
			$mail->Port = 25;
			$mail->From = $mailConfig["username"]; //邮件发送者email地址
			$mail->FromName = $mailConfig["fromname"];
			$mail->AddAddress("$address"); //收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
			$mail->Subject = $title; //邮件标题
			$mail->Body = $content; //邮件内容
			$mail->AltBody = ""; //附加信息，可以省略
		
			$r = $mail->Send();
		}
		catch(Exception $e)
		{
			return false;
		}
	}
	
	/**
	 * 生成随机密码
	 * @param int $l
	 * @return string
	 */
	public static function generateRandPwd($l)
	{
		$str = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,o,v,w,x,y,z,_";
		$a = explode(",",$str);
		$temp = "";
		for($i=0;$i<$l;$i++)
		{
			$k = count($a)-1;
			
			$k = rand(0,$k);
			
			$temp .= $a[$k];
		}
	
		return $temp;
	}
}

?>